﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>续签信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_EmploymentType_FormItem" prop="Entity.EmploymentType">
								<template #label>
									<WtmLabelTooltip label="用工形式" tooltip="用工形式" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.EmploymentType" clearable class="width100">
									<el-option key="全职" value="全职" label="全职"></el-option>
									<el-option key="兼职" value="兼职" label="兼职"></el-option>
									<el-option key="实习" value="实习" label="实习"></el-option>
									<el-option key="外包" value="外包" label="外包"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ContractType_FormItem" prop="Entity.ContractType">
								<template #label>
									<WtmLabelTooltip label="合同类型" tooltip="合同类型" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.ContractType" clearable class="width100">
									<el-option key="固定期限" value="固定期限" label="固定期限"></el-option>
									<el-option key="无固定期限" value="无固定期限" label="无固定期限"></el-option>
									<el-option key="项目合同" value="项目合同" label="项目合同"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>合同信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ContractNo_FormItem" prop="Entity.ContractNo">
								<template #label>
									<WtmLabelTooltip label="合同编号" tooltip="合同编号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ContractNo" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SignDate_FormItem" prop="Entity.SignDate">
								<template #label>
									<WtmLabelTooltip label="签订日期" tooltip="合同签订日期" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.SignDate" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ExpireDate_FormItem" prop="Entity.ExpireDate">
								<template #label>
									<WtmLabelTooltip label="到期日期" tooltip="合同到期日期" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.ExpireDate" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>续签信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="SelectedContractFilesIds_FormItem" prop="SelectedContractFilesIds">
								<template #label>
									<WtmLabelTooltip label="合同文件" tooltip="上传的合同文件" />
								</template>
								<wtm-upload-file v-model="stateData.vmModel.SelectedContractFilesIds" clearable class="width100" :multi="true"></wtm-upload-file>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_ContractCount_FormItem"
								prop="Entity.ContractCount"
								:rules="[{ required: true, message: '合同次数为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="合同次数" tooltip="第几次签订劳动合同" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.ContractCount" :min="0" clearable class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, nextTick } from 'vue';
import { SysPersonContractApi as api } from '/@/views/person/syspersoncontract/api';
import other from '/@/utils/other';
import { ISysPersonContractVm } from '/@/views/person/syspersoncontract/api/interface';
import { IViewModel } from '/@/types/common';

const ci = getCurrentInstance() as any;
const loading = ref(false);
// 编辑时员工字段应该始终禁用以保持数据一致性
const shouldDisablePersonId = ref(true);
// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysPersonContractVm>>({
	vmModel: {
		Entity: {
			ID: null,
			PersonId: null,
			EmploymentType: null,
			ContractType: null,
			ContractNo: null,
			SignDate: null,
			ExpireDate: null,
			ContractCount: 0,
			ContractFiles: null,
		},
		SelectedContractFilesIds: [],
	},

	AllSysPersons: [] as any[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api()
				.edit(stateData.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loading.value = false;
				});
		}
	});
};

// 页面加载时
onMounted(() => {
	loading.value = true;
	if (ci.attrs['wtmdata']) {
		stateData.vmModel.Entity.ID = ci.attrs['wtmdata'].ID;
		stateData.vmModel.Entity.PersonId = ci.attrs['wtmdata'].PersonId; // 自动赋值PersonId
	}
	const promises: Promise<any>[] = [
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((data: any) => other.setValue(stateData.vmModel, data)),
	];
	Promise.allSettled(promises).finally(() => {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: ci.proxy.$t('message._system.common.vm.submit'),
			type: 'primary',
			onClick: onSubmit,
			visible: true,
		},
		{
			text: ci.proxy.$t('message._system.common.vm.cancel'),
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];

	emit('updateHeaderButtons', headerButtons);
};
</script>
